var __UIStory_instance = null;
function getUIStory() { return __UIStory_instance; }

var UIStory = function(){};

var STORY_IDS = [];
var MISSION_IDS = [];

// 关卡列表
var TVStoryCell = cc.Node.extend({

    setIndexOfCells:function(index) {
        this.removeAllChildren();

        var d = TB_story[STORY_IDS[index]];

        // 背景
        var bg = cc.Sprite.create("res/ui/UIStory/img_bg_1.png");
        bg.setAnchorPoint(cc.p(0,0));
        bg.setPosition(cc.p(0,0));
        this.addChild(bg);

        // 名字
        var label = cc.LabelTTF.create(d.name, "Helvetica", 22);
        label.setPosition(cc.p(90, 192));
        label.setAnchorPoint(cc.p(0.5, 0));
        this.addChild(label);

        // 关卡说明
        label = cc.LabelTTF.create(d.desc, "Helvetica", 16, 
                                    cc.size(180, 60), cc.TEXT_ALIGNMENT_LEFT);
        label.setAnchorPoint(cc.p(0, 1));
        label.setPosition(cc.p(10, 100));
        this.addChild(label);

        // 完成度
        label = cc.LabelTTF.create("完成             98%", "Helvetica", 20);
        label.setPosition(cc.p(10, 26));
        label.setAnchorPoint(cc.p(0, 0));
        this.addChild(label);

        // 勋章
        label = cc.LabelTTF.create("勋章              9", "Helvetica", 20);
        label.setPosition(cc.p(10, 4));
        label.setAnchorPoint(cc.p(0, 0));
        this.addChild(label);


        // 图标
        /*
        var image = 'res/Misc/lock.png';
        if (d.icon) { image = 'res/'+d.icon; }

        var sprite = cc.Sprite.create(image);
        sprite.setScale(0.47);
        sprite.setAnchorPoint(cc.p(0,0));
        sprite.setPosition(cc.p(10,15));
        this.addChild(sprite);
        */

        //* 第一项不要顶着边
        this.setPosition(20,0);
    },
});

// 关卡列表
var TVMissionsCell = cc.Node.extend({

    setIndexOfCells:function(index) {
        this.removeAllChildren();

        var d = TB_mission[MISSION_IDS[index]];

        // 背景
        var bg = cc.Sprite.create("res/ui/UIStory/img_bg_2.png");
        bg.setAnchorPoint(cc.p(0,0));
        bg.setPosition(cc.p(0,0));
        this.addChild(bg);

        // 描述
        var msg = d.desc;
        var label = cc.LabelTTF.create(msg, "Helvetica", 20, cc.size(446, 60), cc.TEXT_ALIGNMENT_LEFT);
        label.setPosition(cc.p(10, 106));
        label.setAnchorPoint(cc.p(0,1));
        this.addChild(label);

        // 奖励
        msg = '过关奖励:';
        if(d.award_resource>0) msg = msg + '  资源 +' + d.award_resource;
        if(d.award_glod>0) msg = msg + '  黄金 +' + d.award_glod;
        if(d.award_energy>0) msg = msg + '  指令 +' + d.award_energy;
        label = cc.LabelTTF.create(msg, "Helvetica", 20, cc.size(446, 60), cc.TEXT_ALIGNMENT_LEFT);
        label.setPosition(cc.p(10, 34));
        label.setAnchorPoint(cc.p(0,1));
        this.addChild(label);

        // 按钮
        var menu = cc.Menu.create();
        menu.setPosition(cc.p(0,0));
        this.addChild(menu);

        var btn = cc.MenuItemImage.create('res/ui/comm/btn_simple01.png', 'res/ui/comm/btn_simple02.png', 
            function() { getUIStory().onClickMissionAward(index); }
        );
        btn.setPosition(cc.p(388,26));
        menu.addChild(btn);

        // 按钮文字
        label = cc.LabelTTF.create("领奖", "Helvetica", 20);
        label.setPosition(cc.p(388, 14));
        label.setAnchorPoint(cc.p(0.5, 0));
        this.addChild(label);

        //* 不要靠边
        this.setPosition(0, 5);
    },
});


UIStory.prototype.onDidLoadFromCCB = function()
{
    cc.log("UIStory.onDidLoadFromCCB");
    __UIStory_instance = this;

    // 当前Story
    this._curStoryId = -1;

    // 地图列表
    this._tvStorys = TableViewH.create(
        cc.SCROLLVIEW_DIRECTION_HORIZONTAL, cc.size(640, 230), 
        cc.size(220, 230), 0, TVStoryCell, 
        this.onClickMap );
    this._tvStorys.setPosition(cc.p(0, 580));
    this.uiWindow.addChild(this._tvStorys);

    // 关卡列表
    this._tvMissions = TableViewH.create(
        cc.SCROLLVIEW_DIRECTION_VERTICAL, cc.size(466, 480), 
        cc.size(466, 140), 0, TVMissionsCell,
        this.onClickMission );
    this._tvMissions.setPosition(cc.p(88,82));
    this.uiWindow.addChild(this._tvMissions);
};

// 发送设置主界面数据
UIStory.prototype.updateData = function()
{
    cc.log('UIStory.updateData');

    // 排序地图
    STORY_IDS = [];
    for (var k in TB_story) {
        STORY_IDS.push(k);
    }
    STORY_IDS.sort();
    // 刷新数据
    this._tvStorys.reloadData(STORY_IDS.length);

    // 刷新关卡列表
    this.setCurStory( STORY_IDS[0] );
}

UIStory.prototype.setCurStory = function(storyId)
{
    cc.log('UIStory.setCurStory:'+storyId);
    this._curStoryId = storyId;
    if (storyId == -1) { return; }

    // 关卡列表
    MISSION_IDS = helper.getMissionIdsByStory(storyId);
    MISSION_IDS.sort();
    // 刷新数据
    this._tvMissions.reloadData(MISSION_IDS.length);
}

UIStory.prototype.onOpen = function(isOpen) {
    if (isOpen) {
        this.updateData();
    } else {
    }

    if (this._tvStorys) { this._tvStorys.setTouchEnabled(isOpen); }
    if (this._tvMissions) { this._tvMissions.setTouchEnabled(isOpen); }
}

// 点击地图
UIStory.prototype.onClickMap = function(index)
{
    cc.log('onClickMap:'+index);
    getUIStory().setCurStory( STORY_IDS[index] );
}

// 点击关卡
UIStory.prototype.onClickMission = function(index)
{
    cc.log('onClickMission: index:'+index);

    getUIMain().openUIStory(false);
    getUIMain().startBattle(MISSION_IDS[index]);
}

// 点击关卡领奖
UIStory.prototype.onClickMissionAward = function(index)
{
    cc.log('onClickMissionAward:'+index);
}


